package com.qf.producer.conf;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    /*
        交换机
     */
    @Bean
    public Exchange ex(){
        return ExchangeBuilder
                .topicExchange("boot.ex")
                .durable(true)
                .build();
    }

    /*
        队列
     */
    @Bean
    public Queue que1(){
        return QueueBuilder
                .durable("boot.que1")
                .build();
    }
    @Bean
    public Queue que2(){
        return QueueBuilder
                .durable("boot.que2")
                .build();
    }

    /*
        绑定关系
     */
    @Bean
    public Binding binding1(Queue que1,Exchange ex){
        return BindingBuilder
                .bind(que1)
                .to(ex)
                .with("boot.topic.#")
                .noargs();
    }
    @Bean
    public Binding binding2(Queue que2,Exchange ex){
        return BindingBuilder
                .bind(que2)
                .to(ex)
                .with("boot.topic.*")
                .noargs();
    }

    /*====================================================*/
    @Bean
    public Exchange normalEx(){
        return ExchangeBuilder
                .topicExchange("boot.normalEx")
                .durable(true)
                .build();
    }
    @Bean
    public Queue normalQue(){
        return QueueBuilder
                .durable("boot.normalQue")
                //设置与之对应的死信交换机
                .deadLetterExchange("boot.deadEx")
                //设置将消息转到这个交换机时的路由键
                .deadLetterRoutingKey("boot.dead.abc")
                //设置当前队列中消息的超时时间
//                .ttl(5000)
                //设置该消息队列的最大容量
//                .maxLength(5)
                //设置该队列为惰性队列
                .lazy()
                .build();
    }
    @Bean
    public Binding normalBinding1(Queue normalQue,Exchange normalEx){
        return BindingBuilder
                .bind(normalQue)
                .to(normalEx)
                .with("boot.normal.#")
                .noargs();
    }

    @Bean
    public Exchange deadEx(){
        return ExchangeBuilder
                .topicExchange("boot.deadEx")
                .durable(true)
                .build();
    }
    @Bean
    public Queue deadQue(){
        return QueueBuilder
                .durable("boot.deadQue")
                .build();
    }
    @Bean
    public Binding deadBinding(Queue deadQue,Exchange deadEx){
        return BindingBuilder
                .bind(deadQue)
                .to(deadEx)
                .with("boot.dead.#")
                .noargs();
    }
}
